import 'dart:convert';

import 'package:base_lib/common/network/net.dart';
import 'package:base_lib/common/network/result_body.dart';
import 'package:base_lib/config.dart';
import 'package:base_lib/index.dart';
import 'package:doctor_login_lib/login/entity/doctor_login_entity.dart'
    as DoctorLogin;
import 'package:doctor_login_lib/login/entity/doctor_review_status_entity.dart'
    as DoctorReviewBean;
import 'package:doctor_login_lib/repository/doctor_net_intercept.dart';
import 'package:doctor_login_lib/disease/entity/disease_entity.dart'
    as DoctorDiseaseBean;
import 'package:doctor_login_lib/certification/entity/save_certify_photo_entity.dart'
    as SaveCertifyPhotoBean;
import 'package:doctor_login_lib/certification/entity/doctor_certify_entity.dart'
    as DoctorCertifyInfoBean;
import 'package:doctor_login_lib/repository/doctor_validate_token_intercept.dart';
import '../entity/get_org_name_entity.dart' as GetOrgNameBean;

import '../entity/get_org_info_entity.dart' as GetOrgInfoBean;

class LoginHttp {
  static HttpManager _httpProxyIns;

  static createHttpProxy(
      {mBaseUrl = Config.loginBaseUrl,
      codeName = Config.codeName,
      codeSuccessValue = Config.codeSuccessValue,
      msgName = Config.msgName,
      reqTimeOut = Config.reqTimeOut,
      respTimeOut = Config.respTimeOut,
      logTag = "login"}) {
    _httpProxyIns = HttpManager(mBaseUrl,
        CODE_NAME: codeName,
        CODE_SUCCESS_VALUE: codeSuccessValue,
        MSG_NAME: msgName,
        REQ_TIME_OUT: reqTimeOut,
        RESP_TIME_OUT: respTimeOut,
        LOG_TAG: logTag,
        defaultInterceptors: [DoctorNetIntercept()]);
  }

  /// 得到二维码
  static const INTERFACE_USER_GET_QRCODE = "api/peachUser/qrCode/getQrCode";

  /// 查询资质信息
  static const INTERFACE_GET_CERTIFICATION_BYDOCTORID =
      "api/peachUser/doctorCertify/getDoctorInfoByDoctorID";

  /// 保存资质照片
  static const INTERFACE_SAVE_DOCTOR_CERTIFY_PHOTO =
      "api/peachUser/doctorCertify/saveDoctorCertifyPhoto";

  /// 删除资质照片
  static const INTERFACE_DELETE_DOCTOR_CERTIFY =
      "api/peachUser/doctorCertify/deleteDoctorCertifyPhoto";

  /// 保存医生证件号
  static const INTERFACE_SAVE_DOCTOR_CERTIFYNO =
      "api/peachUser/doctorCertify/saveDoctorCertifyNo";

  /// 保存医生工作照
  static const INTERFACE_SAVE_DOCTOR_PHOTO =
      "api/peachUser/doctorInfo/saveDoctorPhoto";

  /// 提交审核
  static const INTERFACE_DOCTORINFO_COMMITTOREVIEW =
      "api/peachUser/doctorInfo/commitToReview";

  /// 根据医生ID查询专治疾病
  static const String INTERFACE_GET_DOCTOR_DISEASE =
      "api/peachUser/doctorInfo/getDoctorDiseaseByDoctorID";

  /// 保存医生专治疾病
  static const String INTERFACE_SAVE_DOCTOR_DISEASE =
      "api/peachUser/doctorInfo/saveDoctorDisease";

  /// 查看医生审核状态
  static const String INTERFACE_GET_DOCTOR_REVIEW_RESULT =
      "api/peachUser/doctorInfo/getDoctorReviewResult";

  /// 登录
  static const String INTERFACE_LOGIN = "api/peachLogin/doctor/loginByUserPass";

  /// 退出登录
  static const String INTERFACE_LOGOUT = "api/peachUser/doctor/logout";

  /// 请求验证码
  static const String INTERFACE_VERIFICATION_POST =
      "api/peachLogin/doctor/getCode";

  /// 验证码登录  校验验证码
  static const String INTERFACE_VALIDATECODEFORLOGIN =
      "api/peachLogin/doctor/validateCodeForLogin";

  /// 保存密码
  static const String INTERFACE_SAVEUSERPASS =
      "api/peachUser/doctor/saveUserPass";

  /// 修改密码 校验验证码
  static const String INTERFACE_VALIDATE_CODE_FOR_MODIFYPASS =
      "api/peachLogin/doctor/validateCodeForModifyPass";

  /// 查询患者信息
  static const String INTERFACE_GET_PERSON_BY_PERSONID =
      "api/peachUser/personUser/getPersonUserByPersonID";

  /// 保存医生基本信息
  static const String INTERFACE_SAVE_DOCTOR_INFO =
      "api/peachUser/doctorInfo/saveDoctorInfo";

  /// 三方登录
  static const String INTERFACE_DOCTOR_THIRDLOGIN =
      "api/peachLogin/doctor/doctorThirdLoginNewVersion";

  /// 查看用户三方是否注册过
  static const String INTERFACE_IS_THIRDREGISTER =
      "api/peachLogin/doctor/isThirdRegisted";

  /// 生成医生邀请医生链接
  static const String INTERFACE_CREATE_INVITATION_DOCTOR =
      "api/peachLogin/doctor/createDoctorInviteDoctor";

  /// 生成医生邀请个人链接
  static const String INTERFACE_CREATE_INVITATION_PATIENT =
      "api/peachLogin/person/createDoctorInvitePerson";

  /// 苹果鉴权
  static const String INTERFACE_APPLEAUTH = "api/peachLogin/auth/appleAuth";

  /// 查询医院名称集合
  static const String INTERFACE_POST_HOSPITAL_NAME_TWO =
      "api/peachUser/hospitalMng/getHospitalNameById";

  /// 查询医院信息集合
  static const String INTERFACE_GET_HOSPITAL_INFO_BY_IDS =
      "api/peachUser/hospitalMng/getHospitalInfoByIds";

  /// 查询医院信息
  static const String INTERFACE_POST_HOSPITAL =
      "api/peachUser/hospitalMng/getHospital";

  // /**
//  * 校验Token
//  *  http://10.0.0.210:6108/api/peachLogin/doctor/validateDoctorToken
//  */
// @POST("api/peachLogin/doctor/validateDoctorToken")
// @Headers({"url_name:login","Content-Type:application/json","Accept:application/json"})
// Call<BaseEntity> verifyToken(@Body RequestBody body);

  /// 校验Token
  static Future<ResultBody> validateDoctorToken(String token) {
    return _httpProxyIns.postJson('api/peachLogin/doctor/validateDoctorToken',
        (resMap) {
      return resMap;
    },
        jsonData: '{"token":"$token"}',
        interceptors: [DoctorValidateTokenIntercept()]);
  }

  ///实名认证提交审核
  static Future<ResultBody> commitCertificationInfo(String keyID) {
    return _httpProxyIns.postJson(INTERFACE_DOCTORINFO_COMMITTOREVIEW,
        (resMap) {
      return resMap;
    }, jsonData: '{"keyID":$keyID}');
  }

  ///保存医生工作照
  static Future<ResultBody> saveDoctorPhoto(
      String doctorID, String photoUrl, String photoFileID) {
    return _httpProxyIns.postJson(INTERFACE_SAVE_DOCTOR_PHOTO, (resMap) {
      return resMap;
    },
        jsonData:
            '{"doctorID":"$doctorID","photoUrl":"$photoUrl","photoFileID":"$photoFileID"}');
  }

  ///保存医生证件号
  static Future<ResultBody> saveCertificationNumber(String json) {
    return _httpProxyIns.postJson(INTERFACE_SAVE_DOCTOR_CERTIFYNO, (resMap) {
      return resMap;
    }, jsonData: json);
  }

  ///查询资质信息
  static Future<ResultBody<List<DoctorCertifyInfoBean.Data>>>
      getCertificationInfo(String keyID) {
    return _httpProxyIns.get<List<DoctorCertifyInfoBean.Data>>(
        INTERFACE_GET_CERTIFICATION_BYDOCTORID, (resMap) {
      return DoctorCertifyInfoBean.DoctorCertifyEntity.fromJson(resMap).data;
    }, queryParams: Map()..['doctorID'] = keyID);
  }

  ///保存资质照片
  static Future<ResultBody<SaveCertifyPhotoBean.Data>> addCertificationPic(
      Map<String, String> photoData) {
    return _httpProxyIns.postJson<SaveCertifyPhotoBean.Data>(
        INTERFACE_SAVE_DOCTOR_CERTIFY_PHOTO, (resMap) {
      return SaveCertifyPhotoBean.SaveCertifyPhotoEntity.fromJson(resMap).data;
    }, jsonData: json.encode(photoData));
  }

  ///删除资质照片
  static Future<ResultBody> deleteCertificationPic(certifyId) {
    return _httpProxyIns.postJson(INTERFACE_DELETE_DOCTOR_CERTIFY, (resMap) {
      return resMap;
    }, jsonData: '{"keyIDs": $certifyId}');
  }

  ///根据医生ID查询专治疾病
  static Future<ResultBody> getDoctorDisease(keyID) {
    return _httpProxyIns.get(INTERFACE_GET_DOCTOR_DISEASE, (resMap) {
      return resMap;
    }, queryParams: Map()..["doctorID"] = keyID);
  }

  ///保存专治疾病
  static Future<ResultBody> saveDoctorDisease(
      keyID, List<DoctorDiseaseBean.Data> diseaseList) {
    return _httpProxyIns.postJson(INTERFACE_SAVE_DOCTOR_DISEASE, (resMap) {
      return resMap;
    }, jsonData: '{"doctorID":$keyID,"disease":${json.encode(diseaseList)}}');
  }

  ///获取医生审核状态
  static Future<ResultBody<DoctorReviewBean.Data>> getDoctorReviewStatus(
      keyID) {
    return _httpProxyIns.get<DoctorReviewBean.Data>(
        INTERFACE_GET_DOCTOR_REVIEW_RESULT, (resMap) {
      return DoctorReviewBean.DoctorReviewStatusEntity.fromJson(resMap).data;
    }, queryParams: Map()..["doctorID"] = keyID);
  }

  ///医生登录
  static Future<ResultBody<DoctorLogin.Data>> doctorLogin(phone, doctorPass) {
    return _httpProxyIns.postJson<DoctorLogin.Data>(INTERFACE_LOGIN, (resMap) {
      var bean = DoctorLogin.DoctorLoginEntity.fromJson(resMap);
      return bean.data;
    },
        jsonData: json.encode(Map()
          ..["phone"] = phone
          ..["doctorPass"] = doctorPass));
  }

  ///获取验证码
  static Future<ResultBody> getCode(String phone) {
    return _httpProxyIns.postJson(INTERFACE_VERIFICATION_POST, (resMap) {
      return resMap;
    }, jsonData: json.encode(Map()..["phone"] = phone));
  }

  ///医生登录  验证码登录，也是注册的第一步
  static Future<ResultBody<DoctorLogin.Data>> doctorLoginByCode(phone, code) {
    return _httpProxyIns.postJson<DoctorLogin.Data>(
      INTERFACE_VALIDATECODEFORLOGIN,
      (resMap) {
        var bean = DoctorLogin.DoctorLoginEntity.fromJson(resMap);
        return bean.data;
      },
      jsonData: json.encode(Map()
        ..["phone"] = phone
        ..["code"] = code),
    );
  }

  ///注册第二步，设置密码
  static Future<ResultBody> setPassword(phone, pwd) {
    return _httpProxyIns.postJson(INTERFACE_SAVEUSERPASS, (resMap) {
      return resMap;
    },
        jsonData: json.encode(Map()
          ..["phone"] = phone
          ..["doctorPass"] = pwd));
  }

  ///更新医生信息
  static Future<ResultBody<DoctorLogin.Data>> saveDoctorInfo(
      DoctorLogin.DoctorUser doctorUser) {
    return _httpProxyIns.postJson<DoctorLogin.Data>(INTERFACE_SAVE_DOCTOR_INFO,
        (resMap) {
      return DoctorLogin.Data.fromJson(resMap);
    },
        jsonData: JsonUtil.encodeObj(doctorUser.doctorInfo.toJson()
          ..["doctorID"] = doctorUser.keyID
          ..["doctorName"] = doctorUser.doctorName));
  }

  ///查询医院名称集合
  static Future<ResultBody<GetOrgNameBean.Data>> getOrgName(String orgID) {
    return _httpProxyIns.postJson<GetOrgNameBean.Data>(
      INTERFACE_POST_HOSPITAL_NAME_TWO,
      (resMap) => GetOrgNameBean.GetOrgNameEntity.fromJson(resMap).data.first,
      jsonData: '[$orgID]',
    );
  }

  ///查询医院信息集合
  static Future<ResultBody<List<GetOrgInfoBean.Data>>> getOrgInfos(
      List orgIDs) {
    return _httpProxyIns.postJson<List<GetOrgInfoBean.Data>>(
      INTERFACE_GET_HOSPITAL_INFO_BY_IDS,
      (resMap) => GetOrgInfoBean.GetOrgInfosEntity.fromJson(resMap).data,
      jsonData: jsonEncode(orgIDs),
    );
  }

  ///查询医院信息
  static Future<ResultBody<GetOrgInfoBean.Data>> getOrgInfo(String orgID) {
    return _httpProxyIns.get<GetOrgInfoBean.Data>(
      INTERFACE_POST_HOSPITAL,
      (resMap) => GetOrgInfoBean.GetOrgInfoEntity.fromJson(resMap).data,
      queryParams: Map()..['orgID'] = orgID,
    );
  }

  ///得到小程序二维码
  static Future<ResultBody<String>> getQrImg<String>(
      String doctorID, String orgTypeID, String phone) {
    return _httpProxyIns.get(
        INTERFACE_USER_GET_QRCODE, (resMap) => resMap['data'],
        queryParams: Map()
          ..['typeCode'] = 'S'
          ..['biz'] = 'D'
          ..['code'] = doctorID
          ..['orgTypeID'] = orgTypeID
          ..['phone'] = phone);
  }
}
